package com.fdb.basic.framework.web.http;

import java.io.File;
import java.util.Collection;

import javax.servlet.http.Cookie;

import com.fdb.basic.framework.web.controller.UploadAndDownloadSupport;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @description:上传请求Http Client
 * @date:2019年2月25日 下午4:27:01
 * @author:hengyh
 */
public class DefaultUploadHttpClientImpl implements HttpClient<File, String> {

	/**
	 * @description:上传文件客户端
	 * @param url http地址
	 * @param inArgs 上传的文件
	 * @param clazz 返回的参数类对象
	 */
	@Override
	public String httpCall(String url, 
			File inArgs, 
			Class<String> clazz) throws Exception {
		FileSystemResource resource = new FileSystemResource(inArgs);
		MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
		parts.add(UploadAndDownloadSupport.FILE_UPLOAD_NAME, resource);
		
		RestTemplate restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory());
		HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(parts);
		
		ResponseEntity<String> responseEntity = 
				restTemplate.exchange(url, HttpMethod.POST, entity, clazz);
		HttpStatus status = responseEntity.getStatusCode();
		if (status.is2xxSuccessful())
			return responseEntity.getBody();
		else 
			return null;
	}

	@Override
	public void setHeader(String key, String value) {
		
	}

	@Override
	public void setCookie(Collection<Cookie> cookies) {
		
	}

	@Override
	public String httpCall(String url, HttpMethod method, File inArgs, Class<String> clazz, Object... params) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
